from PySide6.QtWidgets import (QFrame)
from PySide6.QtGui import (QMouseEvent)
from PySide6.QtCore import (Qt)
from conf.GameLogin import *
from map.mapManage.mapMgr import *

# 重新 QWidget类, 但是需要继承自 QFrame,  否则修改样式不生效
class MyCanvasWidget(QFrame):
    def __init__(self,form,style="",w=100,h=50,conName="新组件") -> None:
        super().__init__(form)
        self.form = form
        self.styleCopy = style
        self.setStyleSheet(style)
        # 给继承自这个类的画布组件添加名称,  不使用自带的 setObjectName 避免有其他用途时造成冲突
        self.conName = conName
        # self.grabMouse()
        if not w is None:
            self.setMinimumWidth(w)
            self.setMaximumWidth(w)
        if not h is None:
            self.setMinimumHeight(h)
            self.setMaximumHeight(h)

    def mousePressEvent(self, event: QMouseEvent) -> None:
        if event.button() == Qt.MouseButton().LeftButton:
            self.setStyleSheet(".MyCanvasWidget{"+self.styleCopy +";border:1px solid rgb(124,252,0);}")
            self.form.cureChild = self
            self.form.currCheck.setChecked(True)
        return super().mousePressEvent(event)

    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        # 鼠标拖拽
        if event.button() == Qt.MouseButton().LeftButton:
            self.move(self.x(),int(event.position().y()))
        return super().mouseMoveEvent(event)
    
    def mouseReleaseEvent(self, event: QMouseEvent) -> None:
        if event.button() == Qt.MouseButton().LeftButton:
            self.setStyleSheet(self.styleCopy)
        return super().mouseReleaseEvent(event)
    